from django.http import JsonResponse
from user.models import UserProfile
import redis


def test_api(request):
    # 加入分布式锁

    r = redis.Redis(host='122.51.168.137', port=6379, db=0)
    while True:
        try:
            with r.lock('guoxiaonao', blocking_timeout=3):
                # 对 score 字段进行 +1 操作
                u = UserProfile.objects.get(username='guoxiaonao')
                u.score += 1
                u.save()
            break
        except Exception as e:
            print('Lock is failed')

    return JsonResponse({'code': 200})
